<template>
  <div class="profile-page">
    <div class="profile-container">
      <!-- 页面标题 -->
      <div class="page-header">
        <h1 class="page-title">个人中心</h1>
        <p class="page-subtitle">管理您的账户信息和偏好设置</p>
      </div>

      <div class="profile-content">
        <!-- 左侧导航 -->
        <div class="profile-sidebar">
          <nav class="sidebar-nav">
            <button 
              v-for="tab in tabs" 
              :key="tab.id"
              class="nav-item"
              :class="{ active: activeTab === tab.id }"
              @click="activeTab = tab.id"
            >
              <span class="nav-icon">{{ tab.icon }}</span>
              <span class="nav-text">{{ tab.text }}</span>
            </button>
          </nav>
        </div>

        <!-- 右侧内容区域 -->
        <div class="profile-main">
          <!-- 基本信息 -->
          <div v-if="activeTab === 'info'" class="tab-content">
            <div class="info-card">
              <h2 class="card-title">基本信息</h2>
              
              <div class="avatar-section">
                <div class="avatar">
                  <span class="avatar-icon">👤</span>
                </div>
                <button class="change-avatar-btn">更换头像</button>
              </div>

              <div class="info-form">
                <div class="form-row">
                  <div class="form-group">
                    <label class="form-label">用户名</label>
                    <input 
                      v-model="userInfo.username" 
                      type="text" 
                      class="form-input"
                      placeholder="请输入用户名"
                    >
                  </div>
                  <div class="form-group">
                    <label class="form-label">邮箱</label>
                    <input 
                      v-model="userInfo.email" 
                      type="email" 
                      class="form-input"
                      placeholder="请输入邮箱"
                    >
                  </div>
                </div>

                <div class="form-row">
                  <div class="form-group">
                    <label class="form-label">手机号码</label>
                    <input 
                      v-model="userInfo.phone" 
                      type="tel" 
                      class="form-input"
                      placeholder="请输入手机号码"
                    >
                  </div>
                  <div class="form-group">
                    <label class="form-label">注册时间</label>
                    <input 
                      :value="formatDate(userInfo.registerTime)" 
                      type="text" 
                      class="form-input"
                      disabled
                    >
                  </div>
                </div>

                <button class="save-btn" @click="saveProfile">保存修改</button>
              </div>
            </div>
          </div>

          <!-- 安全设置 -->
          <div v-if="activeTab === 'security'" class="tab-content">
            <div class="security-card">
              <h2 class="card-title">安全设置</h2>
              
              <div class="security-item">
                <div class="security-info">
                  <h3>登录密码</h3>
                  <p>定期更改密码可以提高账户安全性</p>
                </div>
                <button class="security-btn" @click="showChangePassword = true">
                  修改密码
                </button>
              </div>

              <div class="security-item">
                <div class="security-info">
                  <h3>账户状态</h3>
                  <p>账户正常使用中</p>
                </div>
                <span class="status-badge active">正常</span>
              </div>
            </div>

            <!-- 修改密码弹窗 -->
            <div v-if="showChangePassword" class="modal-overlay" @click="showChangePassword = false">
              <div class="modal-content" @click.stop>
                <h3>修改密码</h3>
                <div class="password-form">
                  <div class="form-group">
                    <label>当前密码</label>
                    <input type="password" v-model="passwordForm.currentPassword">
                  </div>
                  <div class="form-group">
                    <label>新密码</label>
                    <input type="password" v-model="passwordForm.newPassword">
                  </div>
                  <div class="form-group">
                    <label>确认新密码</label>
                    <input type="password" v-model="passwordForm.confirmPassword">
                  </div>
                  <div class="modal-actions">
                    <button class="btn-cancel" @click="showChangePassword = false">取消</button>
                    <button class="btn-confirm" @click="changePassword">确认修改</button>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- 订单历史 -->
          <div v-if="activeTab === 'orders'" class="tab-content">
            <div class="orders-card">
              <h2 class="card-title">订单历史</h2>
              <div class="orders-list">
                <div v-for="order in orders" :key="order.id" class="order-item">
                  <div class="order-header">
                    <span class="order-id">订单号: {{ order.id }}</span>
                    <span class="order-status" :class="order.status">{{ order.statusText }}</span>
                  </div>
                  <div class="order-content">
                    <div class="order-products">
                      <span class="product-count">{{ order.productCount }} 件商品</span>
                      <span class="order-amount">￥{{ order.amount }}</span>
                    </div>
                    <div class="order-time">{{ order.time }}</div>
                  </div>
                </div>
                
                <div v-if="orders.length === 0" class="empty-orders">
                  <span class="empty-icon">📦</span>
                  <p>暂无订单记录</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useUserStore } from '@/stores/user'
import { storeToRefs } from 'pinia'

const userStore = useUserStore()
const { user } = storeToRefs(userStore)

// 激活的标签页
const activeTab = ref('info')

// 标签页配置
const tabs = [
  { id: 'info', text: '基本信息', icon: '👤' },
  { id: 'security', text: '安全设置', icon: '🔒' },
  { id: 'orders', text: '订单历史', icon: '📦' }
]

// 用户信息
const userInfo = reactive({
  username: '',
  email: '',
  phone: '',
  registerTime: new Date()
})

// 修改密码相关
const showChangePassword = ref(false)
const passwordForm = reactive({
  currentPassword: '',
  newPassword: '',
  confirmPassword: ''
})

// 订单数据
const orders = ref([
  {
    id: '20231215001',
    productCount: 2,
    amount: '2999.00',
    time: '2023-12-15 14:30:25',
    status: 'completed',
    statusText: '已完成'
  },
  {
    id: '20231212001',
    productCount: 1,
    amount: '1599.00',
    time: '2023-12-12 10:15:42',
    status: 'completed',
    statusText: '已完成'
  }
])

// 初始化用户信息
const initUserInfo = () => {
  if (user.value) {
    userInfo.username = user.value.username
    userInfo.email = user.value.email
    userInfo.phone = '138****8888' // 模拟数据
    userInfo.registerTime = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) // 30天前
  }
}

// 保存资料
const saveProfile = () => {
  console.log('保存用户信息:', userInfo)
  // 这里可以调用API保存用户信息
  alert('个人信息保存成功！')
}

// 修改密码
const changePassword = () => {
  if (passwordForm.newPassword !== passwordForm.confirmPassword) {
    alert('两次输入的密码不一致')
    return
  }
  console.log('修改密码:', passwordForm)
  // 这里可以调用API修改密码
  alert('密码修改成功！')
  showChangePassword.value = false
  // 清空表单
  Object.keys(passwordForm).forEach(key => {
    passwordForm[key] = ''
  })
}

// 格式化日期
const formatDate = (date) => {
  return new Date(date).toLocaleDateString('zh-CN')
}

onMounted(() => {
  console.log('个人中心页面加载，用户信息:', user.value)
  initUserInfo()
})
</script>

<style scoped>
.profile-page {
  min-height: calc(100vh - 70px);
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40px 20px;
}

.profile-container {
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  text-align: center;
  margin-bottom: 40px;
  color: white;
}

.page-title {
  font-size: 2.5rem;
  font-weight: 700;
  margin-bottom: 10px;
}

.page-subtitle {
  font-size: 1.1rem;
  opacity: 0.9;
}

.profile-content {
  display: flex;
  gap: 30px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

/* 侧边栏样式 */
.profile-sidebar {
  width: 280px;
  background: #f8f9fa;
  padding: 30px 0;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 20px 30px;
  background: none;
  border: none;
  cursor: pointer;
  transition: all 0.3s ease;
  border-left: 4px solid transparent;
}

.nav-item:hover {
  background: #e9ecef;
}

.nav-item.active {
  background: white;
  border-left-color: #667eea;
  color: #667eea;
  font-weight: 600;
}

.nav-icon {
  font-size: 1.2rem;
}

.nav-text {
  font-size: 1rem;
}

/* 主内容区样式 */
.profile-main {
  flex: 1;
  padding: 40px;
}

.tab-content {
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.card-title {
  font-size: 1.5rem;
  font-weight: 600;
  margin-bottom: 30px;
  color: #333;
}

/* 基本信息样式 */
.info-card {
  max-width: 600px;
}

.avatar-section {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 40px;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea, #764ba2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
}

.change-avatar-btn {
  padding: 8px 16px;
  border: 1px solid #ddd;
  border-radius: 20px;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
}

.change-avatar-btn:hover {
  border-color: #667eea;
  color: #667eea;
}

.form-row {
  display: flex;
  gap: 20px;
  margin-bottom: 25px;
}

.form-group {
  flex: 1;
}

.form-label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #555;
}

.form-input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s ease;
}

.form-input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-input:disabled {
  background: #f8f9fa;
  color: #999;
}

.save-btn {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  border: none;
  padding: 12px 30px;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.save-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

/* 安全设置样式 */
.security-item {
  display: flex;
  justify-content: between;
  align-items: center;
  padding: 20px 0;
  border-bottom: 1px solid #eee;
}

.security-item:last-child {
  border-bottom: none;
}

.security-info {
  flex: 1;
}

.security-info h3 {
  margin: 0 0 5px 0;
  color: #333;
}

.security-info p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.security-btn {
  padding: 8px 16px;
  border: 1px solid #667eea;
  background: white;
  color: #667eea;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.security-btn:hover {
  background: #667eea;
  color: white;
}

.status-badge {
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
}

.status-badge.active {
  background: #d4edda;
  color: #155724;
}

/* 订单历史样式 */
.order-item {
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 15px;
  transition: all 0.3s ease;
}

.order-item:hover {
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.order-id {
  color: #666;
  font-size: 14px;
}

.order-status {
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.order-status.completed {
  background: #d4edda;
  color: #155724;
}

.order-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.product-count {
  color: #666;
}

.order-amount {
  font-weight: 600;
  color: #e74c3c;
}

.order-time {
  color: #999;
  font-size: 14px;
}

.empty-orders {
  text-align: center;
  padding: 60px 20px;
  color: #666;
}

.empty-icon {
  font-size: 3rem;
  margin-bottom: 20px;
  display: block;
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  padding: 30px;
  border-radius: 12px;
  width: 400px;
  max-width: 90vw;
}

.modal-content h3 {
  margin-bottom: 20px;
  color: #333;
}

.password-form .form-group {
  margin-bottom: 20px;
}

.password-form label {
  display: block;
  margin-bottom: 8px;
  color: #555;
}

.password-form input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 6px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 20px;
}

.btn-cancel, .btn-confirm {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn-cancel {
  background: #6c757d;
  color: white;
}

.btn-confirm {
  background: #667eea;
  color: white;
}

.btn-cancel:hover, .btn-confirm:hover {
  opacity: 0.9;
  transform: translateY(-1px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .profile-content {
    flex-direction: column;
  }
  
  .profile-sidebar {
    width: 100%;
    padding: 20px 0;
  }
  
  .sidebar-nav {
    flex-direction: row;
    overflow-x: auto;
  }
  
  .nav-item {
    flex: 1;
    min-width: 120px;
    justify-content: center;
    border-left: none;
    border-bottom: 4px solid transparent;
  }
  
  .nav-item.active {
    border-left: none;
    border-bottom-color: #667eea;
  }
  
  .form-row {
    flex-direction: column;
    gap: 15px;
  }
  
  .profile-main {
    padding: 20px;
  }
}
</style>